<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="1title:浅谈设计模式     设计的目的是对软件系统的规划安排，使之更为可用可靠可维护。而设计系统就是，确定大致结构，确定类的，确定类与类之间的关系职责。学习设计模式也要遵从设计的六大原则：  1）单一职责原则（Single Responsibility Principle,SRP）    一个类只负责一个功能领域的相应职责 2）开闭原则（Open-Closed Principle,OCP）">
<meta property="og:type" content="article">
<meta property="og:title" content="浅谈设计模式">
<meta property="og:url" content="https://www.suichen.top/2020/04/14/%E6%B5%85%E8%B0%88%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/index.html">
<meta property="og:site_name" content="随尘的博客">
<meta property="og:description" content="1title:浅谈设计模式     设计的目的是对软件系统的规划安排，使之更为可用可靠可维护。而设计系统就是，确定大致结构，确定类的，确定类与类之间的关系职责。学习设计模式也要遵从设计的六大原则：  1）单一职责原则（Single Responsibility Principle,SRP）    一个类只负责一个功能领域的相应职责 2）开闭原则（Open-Closed Principle,OCP）">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586765150623.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586765168722.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586766265829.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586766371476.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586766540372.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586767670447.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586767907118.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586768040454.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586770798061.png">
<meta property="og:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586771118965.png">
<meta property="article:published_time" content="2020-04-14T02:04:52.051Z">
<meta property="article:modified_time" content="2020-04-14T02:04:52.052Z">
<meta property="article:author" content="suichen">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://www.suichen.top/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586765150623.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://www.suichen.top/2020/04/14/浅谈设计模式/"/>





  <title>浅谈设计模式 | 随尘的博客</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">随尘的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://www.suichen.top/2020/04/14/%E6%B5%85%E8%B0%88%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="suichen">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="随尘的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">浅谈设计模式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-04-14T10:04:52+08:00">
                2020-04-14
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">title:浅谈设计模式</span><br></pre></td></tr></table></figure>

<p>   设计的目的是对软件系统的规划安排，使之更为可用可靠可维护。而设计系统就是，确定大致结构，确定类的，确定类与类之间的关系职责。学习设计模式也要遵从设计的六大原则：</p>
<p> 1）单一职责原则（Single Responsibility Principle,SRP）</p>
<p>   一个类只负责一个功能领域的相应职责</p>
<p>2）开闭原则（Open-Closed Principle,OCP）</p>
<p>  一个软件应该对扩展开放，对修改关闭，即软件实体应尽量在不修改原有代码的情况下进行扩展</p>
<p>3）里氏替换原则（Liskov Substitution Principle,LSP）</p>
<p>所有的引用基类（父类）的地方必须能够透明地使用其子类的对象。</p>
<p>4）依赖倒置原则（Dependency Inversion Principle,DIP）</p>
<p>抽象不应该依赖于细节，而细节应该依赖于抽象，简而言之，要针对接口编程，而不是针对实现编程。</p>
<p>5）接口隔离原则（Interface Segregation Principle,ISP）</p>
<p>使用多个专门的接口，而不是单一的总接口，即客户端不应该依赖那些它不需要的接口。</p>
<p>6）迪米特法则（Law of Demeter,LOD）</p>
<p>一个软件的实体应尽可能少的与其他实体发生相互作用。</p>
<h6 id="设计模式分类："><a href="#设计模式分类：" class="headerlink" title="设计模式分类："></a>设计模式分类：</h6><p> 1）创建型模式：用于描述怎么创建对象，主要特点是将对象的创建与使用分离开来。包含：工厂模式、单例、原型、抽象工厂、建造者。</p>
<p>2）结构型模式：用于描述如何将类或对象按某种布局组成更大的结构。包含：代理、适配、桥接、装饰、外观、享元、组合等。</p>
<p>3）行为型模式：用于描述类或对象之间怎样相互协作共同完成单个对象都无法完成的任务以及分配相应的职责。包含：模板、 解释器、策略、命令、责任链、状态、观察者、中介者、迭代器、访问者、备忘录。</p>
<p>1.单例模式可以看单例的文章：</p>
<p>2.工厂模式是用工厂类代替new操作的一种模式：</p>
<p>抽象工厂AbstractFactory是接口<br>具体工厂ConcreteFactory是实现类，实现了具体的创建方式<br>客户端Client调用了抽象工厂来创建产品</p>
<p>抽象产品AbstractProduct是接口<br>产品Product是实现类，是具体产品</p>
<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586765150623.png" alt="1586765150623"></p>
<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586765168722.png" alt="1586765168722"></p>
<p>3.建造者模式：使用多个简单的对象一步一步构建成一个复杂的对象。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Builder是接口，定义了构建哪几个部分</span><br><span class="line">ConcreteBuilder是实现了，实现了每个部分的构建过程，并提供getResult方法来得到最终结果对象Product</span><br><span class="line">Director是调用Builder接口的对象</span><br></pre></td></tr></table></figure>



<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586766265829.png" alt="1586766265829"></p>
<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586766371476.png" alt="1586766371476"></p>
<p>4.代理模式：代理模式给某一个对象提供一个代理对象，并由代理对象控制对原对象的引用。</p>
<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586766540372.png" alt="1586766540372"></p>
<p>5.模板模式：定义一个操作中算法的骨架，而将一些步骤延迟到子类中，模板方法使得子类可以不改变算法的结构即可重定义该算法的某些特定步骤。</p>
<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586767670447.png" alt="1586767670447"></p>
<p>Spring中的编码式事务管理使用的TransactionTemplate，也应用了模板模式</p>
<p>6.策略模式：针对一组算法，将每一个算法封装到具有共同接口的独立的类中，从而使得它们可以相互替换。把使用算法的责任和算法本身分割开,委派给不同的对象管理</p>
<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586767907118.png" alt="1586767907118"></p>
<p>7.装饰者模式：动态地给一个对象添加一些额外的职责。就增加功能来说，装饰器模式相比生成子类更为灵活 。</p>
<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586768040454.png" alt="1586768040454"> </p>
<p>8.观察者模式：让多个观察者对象同时监听某一个主题对象。这个主题对象在状态变化时，会通知所有的观察者对象，使他们能够自动更新自己。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Subject：抽象主题（抽象被观察者），抽象主题角色把所有观察者对象保存在一个集合里，每个主题都可以有任意数量的观察者，抽象主题提供一个接口，可以增加和删除观察者对象。</span><br><span class="line">ConcreteSubject：具体主题（具体被观察者），该角色将有关状态存入具体观察者对象，在具体主题的内部状态发生改变时，给所有注册过的观察者发送通知。</span><br><span class="line">Observer：抽象观察者，是观察者者的抽象类，它定义了一个更新接口，使得在得到主题更改通知时更新自己。</span><br><span class="line">ConcrereObserver：具体观察者，实现抽象观察者定义的更新接口，以便在得到主题更改通知时更新自身的状态。</span><br></pre></td></tr></table></figure>

<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586770798061.png" alt="1586770798061"></p>
<p>9.责任链模式：避免请求发送者与接收者耦合在一起，让多个对象都有可能接收请求，将这些对象连接成一条链，并且沿着这条链传递请求，直到有对象处理它为止。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">抽象处理者，定义处理的规范</span><br><span class="line">具体处理者，实现请求的处理</span><br></pre></td></tr></table></figure>

<p><img src="/.top//C:%5CUsers%5Cwll%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1586771118965.png" alt="1586771118965"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/04/01/session%E4%B8%8Ecookie%E7%9A%84%E5%8C%BA%E5%88%AB/" rel="next" title="session与cookie的区别">
                <i class="fa fa-chevron-left"></i> session与cookie的区别
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/04/16/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" rel="prev" title="正则表达式">
                正则表达式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">suichen</p>
              <p class="site-description motion-element" itemprop="description">记录成长</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-6"><a class="nav-link" href="#设计模式分类："><span class="nav-number">1.</span> <span class="nav-text">设计模式分类：</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">suichen</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
